//
//  PublishApi.swift
//  MT
//
//  Created by M on 2019/12/28.
//  Copyright © 2019 张强. All rights reserved.
//

import Foundation

/// 网络
import Moya
import DeviceKit
import SwiftyRSA
import Security

import Result

enum PublishApi {
    
    /// 发布列表
    case post(area: String?, content: String, imgList: [UserMediaListModel]?)
    
    ///
    case delete(id: Int)
}


// MARK: ------------------ Api 参数 ----------------------
extension PublishApi: MTTargetType {

    /// 请求头
    public var headers: [String : String]? {
        guard let accessToken = UserDefaults.MTUserCenter.token.storedString else { return nil }
        return ["Access-Token": accessToken]
    }
    
    /// 基路径
    public var baseURL: URL {
        return URL(string: AuthServerBaseUrl)!
    }
    
    /// 路径
    public var path: String {
        switch self {
            /// 发布
        case .post:
            return "/user-cent/save"
            
        case let .delete(id):
            return "/trends/delete/\(id)"
        }
    }
    
    /// 请求方法
    public var method: Moya.Method {
        switch self {
        case .delete:   return .get
        default:        return .post
        }
    }
    
    /// 参数
    var params: [String: Any]? {
        
        switch self {
            /// 热门列表
        case let .post(area, content, imgList):
            var dict: [String: Any] = ["content": content]
            if let area = area {
                dict["area"] = area
            }
            if let imgList = imgList {
                dict["imgList"] = imgList.toJSON()
            }
            return dict
            
        case .delete:
            return nil
        }
        
    }
    
}
